package com.wan37.protobuf.util;

import com.google.protobuf.Message;
import com.wan37.protobuf.analysis.ParseMap;
import com.wan37.protobuf.constant.PtoNum;
import com.wan37.protobuf.message.internal.Internal;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;


/**
 * protobuf的工具类。
 *
 * @author luoyong
 */
public class ServerProtoUtils {

    private ServerProtoUtils() {

    }

    /**
     * 将消息体包装成服务器间GTransfer消息体, 并转换成ByteBuf对象。
     *
     * @param dest
     * @param netId
     * @param userId
     * @param ptoNum
     * @param msg
     * @param sceneId
     * @return
     */
    public static ByteBuf pack2Server(Internal.DestType dest, long netId, String userId, int ptoNum, Message msg, String sceneId) {

        //通过Builder构建gTransfer对象。
        Internal.GTransfer.Builder gtb = Internal.GTransfer.newBuilder();
        gtb.setDest(dest)
                .setNetId(netId)
                .setUserId(userId)
                .setPtoNum(ptoNum)
                .setMsg(msg.toByteString())
                .setSceneId(sceneId);

        byte[] bytes = gtb.build().toByteArray();
        int length = bytes.length;
        int protoNum = PtoNum.GTRANSFER;

        ByteBuf buf = Unpooled.buffer(8 + length);
        buf.writeInt(length);
        buf.writeInt(protoNum);
        buf.writeBytes(bytes);

        return buf;
    }


    /**
     * 将消息体包装成服务器间GTransfer消息体, 并转换成ByteBuf对象。
     *
     * @param dest
     * @param netId
     * @param userId
     * @param ptoNum
     * @param msg
     * @return
     */
    public static ByteBuf pack2Server(Internal.DestType dest, long netId, String userId, int ptoNum, Message msg) {

        //通过Builder构建gTransfer对象。
        Internal.GTransfer.Builder gtb = Internal.GTransfer.newBuilder();
        gtb.setDest(dest)
                .setNetId(netId)
                .setUserId(userId)
                .setPtoNum(ptoNum)
                .setMsg(msg.toByteString());

        byte[] bytes = gtb.build().toByteArray();
        int length = bytes.length;
        int protoNum = PtoNum.GTRANSFER;

        ByteBuf buf = Unpooled.buffer(8 + length);
        buf.writeInt(length);
        buf.writeInt(protoNum);
        buf.writeBytes(bytes);

        return buf;
    }


    /**
     * 将消息体包装成服务器间GTransfer消息体, 并转换成ByteBuf对象。
     * netId为空。
     *
     * @param dest
     * @param userId
     * @param ptoNum
     * @param msg
     * @return
     */
    public static ByteBuf pack2Server(Internal.DestType dest, String userId, int ptoNum, Message msg) {

        Internal.GTransfer.Builder gtb = Internal.GTransfer.newBuilder();
        gtb.setDest(dest)
                .setUserId(userId)
                .setPtoNum(ptoNum)
                .setMsg(msg.toByteString());

        byte[] bytes = gtb.build().toByteArray();
        int length = bytes.length;
        int protoNum = PtoNum.GTRANSFER;

        ByteBuf buf = Unpooled.buffer(8 + length);
        buf.writeInt(length);
        buf.writeInt(protoNum);
        buf.writeBytes(bytes);
        return buf;
    }


    /**
     * 将消息封装成发往客户端的消息, 返回ByteBuf对象。
     *
     * @param msg
     * @return
     */
    public static ByteBuf pack2Client(Message msg) {

        byte[] bytes = msg.toByteArray();
        int length = bytes.length;
        Integer ptoNum = ParseMap.getPtoNum(msg);
        if (ptoNum == null) {
            throw new NullPointerException("ptoNum is null.");
        }

        ByteBuf buf = Unpooled.buffer(8 + length);
        buf.writeInt(length);
        buf.writeInt(ptoNum);
        buf.writeBytes(bytes);

        return buf;
    }
}











